package DBLayer;
import model.*;

import java.sql.*;

public class DBContactPerson {

	private  Connection con;
	
	public DBContactPerson() {
		con = DBConnection.getInstance().getDBcon();
	}
	
	public ContactPerson findContactPerson(int ID) {
	 	String wClause = " contactPersonID = '" + ID + "'";
	 	return singleWhere(wClause);
	}
	
	private ContactPerson singleWhere(String wClause) {	
		ResultSet results;
		ContactPerson conObj = new ContactPerson();
	  	String query =  buildQuery(wClause);
	  	System.out.println("DBPContactPerson -singelWhere " + query); 
	  	try { // read from project
	    	Statement stmt = con.createStatement();
	       	stmt.setQueryTimeout(5);
	      	results = stmt.executeQuery(query);

	     	if( results.next() ){
	     		conObj = buildContactPerson(results); 
	      	}
	        stmt.close();
		}
	  	catch(Exception e){
	    	System.out.println("Query exception - select project : "+e);
	      	e.printStackTrace();
	  	}
		return conObj;
	}
	
	private ContactPerson buildContactPerson(ResultSet results) {
		ContactPerson conObj = new ContactPerson();
	    DBSupplier dbs = new DBSupplier();   
		try {
			conObj.setID(results.getInt("ContactPersonID"));
	       	conObj.setName(results.getString("contactName"));
	       	conObj.setLastName(results.getString("contactLastName"));
	       	conObj.setEmail(results.getString("contactEmail"));
	       	conObj.setTelephone(results.getString("contactTelephone")); 
		}
		catch(Exception e) {
	    	System.out.println("building ContactPerson object");
	    }
	    return conObj; 
	}
	
	private String buildQuery(String wClause) {
		String query="SELECT contactPersonID, contactName, contactLastName, contactEmail, contactTelephone FROM contactPerson";	            
	           
		if (wClause.length()>0) {
			query = query + " where " + wClause;
		}
		return query;
	}
	
}  
	    
